package cn.flkj.evidence.controllers;


import cn.flkj.evidence.EvidenceImpl;
import cn.flkj.evidence.modles.Evidence;
import cn.flkj.evidence.repositories.EvidenceMapper;
import cn.flkj.evidence.util.Result;
import cn.flkj.evidence.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.SignatureException;
import java.util.concurrent.ExecutionException;

/**
 * 1. 存证机构签名
 */


@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/depositor") // 存证
public class DepositorController {
    @Resource
    private EvidenceMapper evidenceMapper;
    private static final String USER = "user.jks";
    private static final String ARBITRATOR = "arbitrator.jks";
    private static final String DEPOSITOR = "depositor.jks";
    private static final String UNIVERSAL_PASSWORD = "123456"; //通用密码，防止写错
    private static final String EVIDENCE_SIGNERS_ADDRESS ="0x0f74e2529e561b2749034a3d235da8cf04ab8bea";  // 工厂合约地址

    private final EvidenceImpl evidenceImpl;

    public DepositorController(EvidenceImpl evidenceImpl) {
        this.evidenceImpl = evidenceImpl;
    }

    @PostMapping("/depositorSignature")
    public Result<Boolean> depositorSignature(@RequestParam("evidenceMd5") String evidence_md5){
       Evidence evidence = evidenceMapper.findByEvidenceMd5(evidence_md5);
       String newEvidenceAddress = evidence.getEvidence_address();
       String evidenceHash = evidence.getEvidence_md5();
       boolean flag = false;
       try {
           flag = evidenceImpl.sendSignatureToBlockChain(
                   DEPOSITOR,
                   UNIVERSAL_PASSWORD,
                   UNIVERSAL_PASSWORD,
                   newEvidenceAddress,
                   evidenceHash
           );
       }catch ( InterruptedException| ExecutionException | SignatureException e){
           log.error("签名错误 ： "+e);
       }

        return ResultUtil.success(flag);
    }



}
